package ltd.dujiabao.eduservice.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import ltd.dujiabao.commonutils.Result;
import ltd.dujiabao.eduservice.entity.EduVideo;
import ltd.dujiabao.eduservice.service.EduVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author dujiabao
 * @since 2021-04-21
 */
@Api(value = "")
@CrossOrigin
@RestController
@RequestMapping("/eduservice/video")
@Slf4j
public class EduVideoController {
  @Autowired
  EduVideoService eduVideoService;

  @ApiOperation("添加小节")
  @PostMapping("/")
  public Result addVideo(@ApiParam(name = "eduVideo", value = "小节信息", required = true) @RequestBody EduVideo eduVideo) {
    boolean isSuccess = eduVideoService.save(eduVideo);
    if (isSuccess) {
      log.info("添加小节成功" + eduVideo);
      return Result.success();
    } else {
      log.error("添加小节失败" + eduVideo);
      return Result.failure();
    }
  }

  @ApiOperation("获取小节")
  @GetMapping("/{id}")
  public Result getVideo(@ApiParam(name = "id", value = "小节id", required = true) @PathVariable String id) {
    EduVideo chapter = eduVideoService.getById(id);
    log.info("获取小节 " + id + " 内容" + chapter);
    return Result.success().addData("data", chapter);
  }

  @ApiOperation("删除小节")
  @DeleteMapping("/{id}")
  public Result removeVideo(@ApiParam(name = "id", value = "小节id", required = true) @PathVariable String id) {
    boolean isSuccess = eduVideoService.removeVideo(id);
    if (isSuccess) {
      log.info("删除小节" + id + "成功");
      return Result.success();
    } else {
      log.error("删除小节" + id + "失败");
      return Result.failure();
    }
  }

  @ApiOperation("修改小节")
  @PutMapping("/{id}")
  public Result updateVideo(@ApiParam(name = "id", value = "小节id", required = true) @PathVariable String id,
                            @ApiParam(name = "eduVideo", value = "小节信息", required = true) @RequestBody EduVideo eduVideo) {
    eduVideo.setId(id);
    boolean isSuccess = eduVideoService.updateById(eduVideo);
    if (isSuccess) {
      log.info("修改小节 " + id + " 成功" + eduVideo);
      return Result.success();
    } else {
      log.error("修改小节 " + id + " 失败" + eduVideo);
      return Result.failure();
    }
  }
}

